#include "control.h"

ControlObjectDef  CO;
extern uint16_t theNum;
//初始化设置参数结构体
void controlInit(void)
{
	CO.commonpoint.x = 0;
	CO.commonpoint.y = 0;

	CO.cornerPoint[0].x=225;
	CO.cornerPoint[0].y=165;
	CO.cornerPoint[1].x=215;
	CO.cornerPoint[1].y=-270;
	CO.cornerPoint[2].x=-235;
	CO.cornerPoint[2].y=-270;
	CO.cornerPoint[3].x=-240;
	CO.cornerPoint[3].y=180;

	CO.workStatus = FREE;
	CO.loop = 0;
	CO.pointNum=1;
	CO.pointindex=0;

	CO.spinspeed=5000;
	CO.fineunit=40;
	CO.fineindex=0;

	CO.targetPointer = &CO.commonpoint;

	CO.pauseFlag = 0;
	CO.resetFlag = 0;

	set = 0;
}

/*------------------------------------ 设定原点 -----------------------------------*/
void setPulseOrigin(void)
{
	HAL_TIM_Base_Stop_IT(&htim2);
	CO.commonpoint.x=0;
	CO.commonpoint.y=0;
	XSO.currentPulsePos=0;
	YSO.currentPulsePos=0;
	XSO.taragetPulsePos=0;
	YSO.taragetPulsePos=0;
	set = 1;
    HAL_TIM_Base_Start_IT(&htim2);
}

/*------------------------------------ 设定四个角点 -----------------------------------*/
void setPulseCorner(int index)
{
	markCoordNum();
	CO.cornerPoint[index].x = XSO.currentPulsePos;
	CO.cornerPoint[index].y = YSO.currentPulsePos;
}

void markCoordNum(void)
{
	switch(CO.fineindex)
	{
		case 0 : set = 2;   break;  //右上
		case 1 : set = 3;   break;  //右下
		case 2 : set = 4;   break;  //左下
		case 3 : set = 5;  break;  //左上
	}

}


void setSpinSpeed(int upval)
{
	CO.spinspeed-=upval;
    if(CO.spinspeed<=3000) CO.spinspeed =3000;
    if(CO.spinspeed>=100000) CO.spinspeed =100000;
    __HAL_TIM_SET_AUTORELOAD(&htim3,CO.spinspeed);
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, CO.spinspeed*0.5);
    __HAL_TIM_SET_AUTORELOAD(&htim4, CO.spinspeed);
    __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, CO.spinspeed*0.5);
}

void frameWrap1(void)
{
	HAL_TIM_Base_Stop_IT(&htim2);
	YSO.stepDelay = 0;
	XSO.stepDelay = 0;
	CO.pauseFlag = 0;
	CO.workStatus =FREE;
	CO.targetPointer = &CO.cornerPoint[0];
	CO.loop = -1;
	CO.pointindex=-1;
	CO.pointNum=4;
	HAL_TIM_Base_Start_IT(&htim2);
}

void frameWrap2(void)
{
	//closePWM();
	HAL_TIM_Base_Stop_IT(&htim2);
	YSO.stepDelay = 0;
	XSO.stepDelay = 0;
	CO.pauseFlag = 0;
	CO.workStatus =FREE;
	CO.targetPointer = &g_rectangle[0];
	CO.loop = -1;
	CO.pointindex=-1;
	CO.pointNum=theNum;
	HAL_TIM_Base_Start_IT(&htim2);
	//openPWM();
}

void goToSinglePoint(int x,int y)
{
	//closePWM();
	HAL_TIM_Base_Stop_IT(&htim2);
	YSO.stepDelay = 0;
	XSO.stepDelay = 0;
	CO.pauseFlag = 0;
	CO.workStatus =FREE;
	CO.commonpoint.x=x;
	CO.commonpoint.y=y;
	CO.pointindex=0;
	CO.pointNum=1;
	CO.targetPointer = &CO.commonpoint;
	HAL_TIM_Base_Start_IT(&htim2);
	//openPWM();
}

void goHome(void)
{
	//closePWM();
	goToSinglePoint(0,0);
	//openPWM();
}

/*------------------------------------ 开关PWM中断 -----------------------------------*/
 void openPWM(void)
 {
	 HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_1);
	 HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
	 HAL_TIM_PWM_Start_IT(&htim4,TIM_CHANNEL_1);
	 HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
 }
 void closePWM(void)
 {
	//XSO.stepStatus = FREE;
	//YSO.stepStatus = FREE;
	HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
	HAL_TIM_PWM_Stop_IT(&htim3,TIM_CHANNEL_1);
	HAL_TIM_PWM_Stop(&htim4,TIM_CHANNEL_1);
	HAL_TIM_PWM_Stop_IT(&htim4,TIM_CHANNEL_1);
 }

